package imserver.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import imserver.beans.GroupInfo;
import imserver.common.util.BaseResponse;
import imserver.common.util.DaoException;
import imserver.common.util.StringUtil;
import imserver.service.GroupService;

/**
 * 分组管理
 * @author zhanghongbin01
 *
 */
@Controller
@RequestMapping("/member")
public class GroupController {
	
	@Resource
	private GroupService groupService;
	private Logger log = Logger.getLogger(GroupController.class);
	
	/**
	 * 新建一个分组
	 * @return
	 */
	@RequestMapping("/create")
	@ResponseBody
	public BaseResponse create(HttpServletRequest request){
		BaseResponse response = new BaseResponse();
		String groupName = request.getParameter("groupName");
		int owner = Integer.parseInt(request.getParameter("owner"));
		response.setStatus(200);
		if(StringUtil.isNullOrEmpty(groupName)){
			response.setMsg("组名不可以为空哦！");
			response.setErrorno(-1);
			log.warn("组名不可以为空哦！");
		}else if(owner <= 0){
			response.setMsg("创建者的id不可以少哦！");
			response.setErrorno(-1);
			log.warn("创建者的id不可以少哦！");
		}else{
			GroupInfo groupInfo = new GroupInfo();
			groupInfo.setCreateTime(new Date());
			groupInfo.setName(groupName);
		}
		return response;
	}
	
	/**
	 * 添加好友到分组
	 * @return
	 * @throws DaoException 
	 */
	@RequestMapping("/add")
	public BaseResponse addMember(HttpServletRequest request) throws DaoException{
		BaseResponse response = new BaseResponse();
		int groupId = Integer.parseInt(request.getParameter("groupId"));
		String memberNum = request.getParameter("memberNum");
		response.setStatus(200);
		if(groupId <= 0){
			response.setErrorno(-1);
			response.setMsg("分组的ID必须要并且正确哦！");
		}else if(StringUtil.isNullOrEmpty(memberNum)){
			response.setErrorno(-1);
			response.setMsg("要添加的好友的账号不能为空！");
		}else{
			groupService.addMember(groupId, memberNum);
			response.setErrorno(0);
			response.setMsg("ok");
		}
		
		return response;
	}
	
	/**
	 * 从分组删除一个好友
	 * @return
	 * @throws DaoException 
	 */
	@RequestMapping("/del_member")
	@ResponseBody
	public BaseResponse delMember(HttpServletRequest request) throws DaoException{
		BaseResponse response = new BaseResponse();
		int groupId = Integer.parseInt(request.getParameter("groupId"));
		int memberId = Integer.parseInt(request.getParameter("memberNum"));
		response.setStatus(200);
		if(groupId <= 0){
			response.setErrorno(-1);
			response.setMsg("分组的ID必须要并且正确哦！");
		}else if(memberId <= 0){
			response.setErrorno(-1);
			response.setMsg("要删除的好友的ID不能为空！");
		}else{
			groupService.delMember(groupId, memberId);
			response.setErrorno(0);
			response.setMsg("ok");
		}
		return response;
	}
}
